<template>
  <el-container class="home_container">
    <el-header>
      <div>
        <img
          src="https://tse3-mm.cn.bing.net/th/id/OIP-C.YqlpAXi3u_n1-tlScLgwdgHaFG?w=234&h=180&c=7&r=0&o=5&dpr=1.3&pid=1.7"
          alt=""
        />
        <span>电商后台管理系统</span>
      </div>
      <el-button @click="loginout">退出</el-button>
    </el-header>

    <el-container>
      <el-aside :width="isCollapse ? '64px' : '200px'">
        <div class="toggle-button" @click="toggleCollapse">|||</div>
        <el-menu
          :default-active="activePath"
          background-color="#fff"
          text-color="#ccc"
          active-text-color="#000"
          unique-opened
          :collapse="isCollapse"
          :collapse-transition="false"
          :router="true"
        >
          <!-- 一级菜单 -->
          <el-submenu
            :index="item.id + ''"
            v-for="(item, index) in menuList"
            :key="item.id"
          >
            <!-- 一级菜单模板 -->
            <template slot="title">
              <!-- 图标 -->
              <i :class="iconsObj[item.id]" id="iconfont"></i>
              <!-- 文本 -->
              <span>{{ item.authName }}</span>
            </template>
            <!-- 二级菜单 -->
            <el-menu-item
              :index="'/' + subitem.path"
              v-for="(subitem, index) in item.children"
              :key="subitem.id"
              @click="saveNavState('/' + subitem.path)"
            >
              <template slot="title">
                <!-- 图标 -->
                <i class="el-icon-menu"></i>
                <!-- 文本 -->
                <span>{{ subitem.authName }}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  created() {
    this.getMenuList()
    this.activePath = window.sessionStorage.getItem('activePath')
  },
  data() {
    return {
      menuList: [],
      isCollapse: false,
      iconsObj: {
        125: 'iconfont icon-gouwuche',
        103: 'iconfont icon-tijikongjian',
        101: 'iconfont icon-shangpin',
        102: 'iconfont icon-danju',
        145: 'iconfont icon-BIshujuzhongxin',
      },
      //未激活的链接地址
      activePath: '',
    }
  },
  methods: {
    loginout() {
      window.sessionStorage.clear()
      this.$router.push('/login')
    },
    async getMenuList() {
      const { data: res } = await this.$http.get('menus')
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      console.log(res)
      this.menuList = res.data
    },
    toggleCollapse() {
      this.isCollapse = !this.isCollapse
    },
    // 保存链接的激活状态
    saveNavState(activePath) {
      window.sessionStorage.setItem('activePath', activePath)
      this.activePath = activePath
    },
  },
}
</script>

<style lang="less" scoped>
.home_container {
  height: 100%;
}
.el-header {
  border-bottom: 1px solid #ccc;
  display: flex;
  justify-content: space-between;
  padding: 0;
  div {
    display: flex;
    align-items: center;
    img {
      height: 100%;
    }
  }
  el-button {
    border: 0;
  }
}
.el-aside {
  border-right: 1px solid #ccc;
}
.toggle-button {
  background: #ccc;
  font-size: 10px;
  line-height: 24px;
  color: #fff;
  text-align: center;
  letter-spacing: 0.2em;
  cursor: pointer;
}
#iconfont {
  color: #ccc;
}
</style>
